
import pymongo

# 连接字符串,指定用户名和密码
conn_str = "mongodb://admin:123456@localhost:27017/"
# 连接到MongoDB,使用用户名和密码进行认证 
client = pymongo.MongoClient(conn_str)
# 连接到数据库mydatabase
db = client["mydatabase"]
# 连接到集合mycollection
collection = db["mycollection"]
# 插入一条数据
collection.insert_one({"name": "Jack"})
# 查找所有数据
cursor = collection.find()
for document in cursor:
    print(document)
# 关闭连接 
client.close()
